#include "stdio.h"

int win_by_one(char may_field_[3][3], char who){
    char may_field[3][3];
    for (int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            may_field[i][j] = may_field_[i][j];
        }
    }
	for (int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if (may_field[i][j] == '*' || may_field[i][j] == '-'){
				may_field[i][j] = who;

				if (isWon(may_field)){
					return ((i+1)*10) + (j+1);
				} else {
					may_field[i][j] = '-';
				}
			}
		}
	}
	return -1;
}

int make_turn(char field[3][3], char who, char enemy){
	char may_field[3][3];

/*
	{'X','O'} - Already turned by player/bot before
	'-' - False tryed
	'+' -
*/

	bool isCloned = false;

	//cloning
	if (!isCloned){
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){
				may_field[i][j] = field[i][j];
			}
		}
	}


	if(win_by_one(may_field, who) != -1){
		return win_by_one(may_field, who); //win by who
	}
	else if(win_by_one(may_field, enemy) != -1){
		return win_by_one(may_field, enemy); //win by enemy
	}

	if(may_field[1][1] == '*' || may_field[1][1] == '-'){ // turn center
		return 22;
	}

	for (int i = 0; i < 3; i++){ // turn to corner
		for (int j = 0; j < 3; j++){
			if ((i == 0 || i == 2) && (j == 0 || j == 2) && (may_field[i][j] == '*' || may_field[i][j] == '-')){
				return ((i+1)*10) + (j+1);

			}
		}
	}

	for (int i = 0; i < 3; i++){ // turn to any pos
		for (int j = 0; j < 3; j++){
			if (may_field[i][j] == '*' || may_field[i][j] == '-'){
				return ((i+1)*10) + (j+1);
			}
		}
	}
	return 0;
}
